package io.renren.modules.courseSmall.service.impl;

import com.sun.org.apache.regexp.internal.RE;
import io.renren.common.utils.R;
import io.renren.modules.courseSmall.dao.courseSmallDao;
import io.renren.modules.courseSmall.entity.CourseSmall;
import io.renren.modules.courseSmall.entity.CourseSmallAndTeacherEntity;
import io.renren.modules.courseSmall.entity.CourseSmallClassification;
import io.renren.modules.courseSmall.service.courseSmallService;
import io.renren.modules.sys.dao.TokenDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class courseSmallServiceImpl implements courseSmallService {

    @Autowired
    courseSmallDao courseSmallDao;

    @Autowired
    TokenDao tokenDao;

    @Override
    public CourseSmallAndTeacherEntity getCourseInfoById(Map map) {
        CourseSmallAndTeacherEntity courseSmallAndTeacherEntity = courseSmallDao.getCourseInfoById(map);
        courseSmallAndTeacherEntity.setNickName(new String(courseSmallAndTeacherEntity.getNickNameB()));
        return courseSmallAndTeacherEntity;
    }


    @Override
    public List getCourseSmall(Map map) {
        return courseSmallDao.getCourseSmall(map);
    }

    @Override
    public List getRandomCourseSmall() {
        List idCount = courseSmallDao.getCountCourseSmall();
        List list = new ArrayList();
        if(idCount.size() <= 0){
            return new ArrayList();
        }
        if(idCount.size() < 6){
            List allCourseList = courseSmallDao.getAllCourseSmall();
            return allCourseList;
        }
        List randomIdList = getRandom(idCount.size());
        Map map = new HashMap();
        for(int i = 0;i<randomIdList.size();i++){
            int c = Integer.parseInt(randomIdList.get(i).toString())-1;
            int index = Integer.parseInt(idCount.get(c).toString());
            map.put("id",index);
            CourseSmall courseSmall = courseSmallDao.getCourseById(map);
            list.add(courseSmall);
        }
        return list;
    }

    @Override
    public void addSeeCount(Map map) {
        courseSmallDao.addSeeCount(map);
    }

    @Override
    public void addCourseSmallCollection(Map map) {
        Long userId = tokenDao.getUserIdByToken(map.get("token").toString());
        map.put("userId",userId);
        courseSmallDao.addCourseSmallCollection(map);
    }

    @Override
    public void delCourseSmallCollection(Map map) {
        Long userId = tokenDao.getUserIdByToken(map.get("token").toString());
        map.put("userId",userId);
        courseSmallDao.delCourseSmallCollection(map);
    }

    @Override
    public List getCourseSmallCollection(Map map) {
        Long userId = tokenDao.getUserIdByToken(map.get("token").toString());
        map.put("userId",userId);
        //查看我的收藏的微课程的id列表
        List courseSmallIdList = courseSmallDao.getCourseSmallIdList(map);
        List list = new ArrayList();
        if(courseSmallIdList.size()!= 0){
            for(int i=0;i<courseSmallIdList.size();i++){
                map.put("id",courseSmallIdList.get(i));
                CourseSmall courseSmall =  courseSmallDao.getCourseById(map);
                list.add(courseSmall);
            }
        }

        return list;
    }

    @Override
    public CourseSmall getCourseById(Map map) {
        return courseSmallDao.getCourseById(map);
    }

    @Override
    public List<CourseSmallClassification> getCourseSmallClassification(int id) {
        //先获取的第一层的分类列表
        List<CourseSmallClassification> firstClassificationList = courseSmallDao.getFirstCourseSmallClassification(id);
        for(CourseSmallClassification courseSmallClassification : firstClassificationList){
            List<CourseSmallClassification> secondClassificationList = this.getCourseSmallClassification(courseSmallClassification.getId());
            if(secondClassificationList.size() != 0){
                courseSmallClassification.setChildClassificationList(secondClassificationList);
            }
        }
        return firstClassificationList;
    }

    @Override
    public List getCourseSmallByClassification(Map map) {
        return courseSmallDao.getCourseSmallByClassification(map);
    }

    public List getRandom(int count){
        // 生成 [0-n) 个不重复的随机数
        // list 用来保存这些随机数
      Set set = new HashSet();
        Random rand = new Random();
        int c =0;
        while(c+set.size() != c+6){
            int i = rand.nextInt(count-1)+1;
            set.add(i);
            c++;
        }
        List list = new ArrayList(set);
    return list;
    }

}
